package common.util.excel.easyexcel;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import org.apache.commons.collections.CollectionUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.ArrayList;
import java.util.List;


/**
 * description:
 *
 * @author yanxf@winning.com.cn
 * @date 2021/11/24 13:59
 */
public class MergeStrategy extends AbstractMergeStrategy{
    //合并坐标集合
    private List<CellRangeAddress> cellRangeAddresss;
    //构造
    public MergeStrategy() {
        List<CellRangeAddress> list = new ArrayList<>();
        //合并 单元格坐标
     /*   CellRangeAddress item1 = new CellRangeAddress(0, 0, 0, 1);
        CellRangeAddress item2 = new CellRangeAddress(0, 0, 2, 3);
        CellRangeAddress item3 = new CellRangeAddress(3, 3, 4, 5);*/
        //合并 单元格坐标

        CellRangeAddress item0 = new CellRangeAddress(0, 1, 0, 0);
        CellRangeAddress item1 = new CellRangeAddress(0, 0, 1, 2);
        CellRangeAddress item2 = new CellRangeAddress(0, 0, 3, 4);
        CellRangeAddress item3 = new CellRangeAddress(0, 0, 5, 6);
        CellRangeAddress item4 = new CellRangeAddress(0, 0, 7, 8);
        CellRangeAddress item5 = new CellRangeAddress(0, 0, 9, 10);
        CellRangeAddress item6 = new CellRangeAddress(0, 0, 11, 12);
        CellRangeAddress item7 = new CellRangeAddress(0, 0, 13, 14);
        CellRangeAddress item8 = new CellRangeAddress(0, 0, 15, 16);
        CellRangeAddress item9 = new CellRangeAddress(0, 0, 17, 18);

        CellRangeAddress item10 = new CellRangeAddress(0, 0, 19, 20);
        CellRangeAddress item11 = new CellRangeAddress(0, 0, 21, 22);
        CellRangeAddress item12 = new CellRangeAddress(0, 0, 23, 24);
        CellRangeAddress item13 = new CellRangeAddress(0, 0, 25, 26);
        CellRangeAddress item14 = new CellRangeAddress(0, 0, 27, 28);
        CellRangeAddress item15 = new CellRangeAddress(0, 0, 29, 30);
        CellRangeAddress item16 = new CellRangeAddress(0, 0, 31, 32);
        CellRangeAddress item17 = new CellRangeAddress(0, 0, 33, 34);
        CellRangeAddress item18 = new CellRangeAddress(0, 0, 35, 36);
        CellRangeAddress item19 = new CellRangeAddress(0, 0, 37, 38);
        CellRangeAddress item20 = new CellRangeAddress(0, 0, 39, 40);
        CellRangeAddress item21 = new CellRangeAddress(0, 0, 41, 42);
        CellRangeAddress item22 = new CellRangeAddress(0, 0, 43, 44);

        list.add(item0);
        list.add(item1);
        list.add(item2);
        list.add(item3);
        list.add(item4);
        list.add(item5);
        list.add(item6);
        list.add(item7);
        list.add(item8);
        list.add(item9);


        list.add(item10);
        list.add(item11);
        list.add(item12);
        list.add(item13);
        list.add(item14);
        list.add(item15);
        list.add(item16);
        list.add(item17);
        list.add(item18);
        list.add(item19);
        list.add(item20);
        list.add(item21);
        list.add(item22);
        this.cellRangeAddresss = list;
    }
    /**
     * merge
     * @param sheet
     * @param cell
     * @param head
     * @param relativeRowIndex
     */
    @Override
    protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {
        //合并单元格
        /**
         *  ****加个判断:if (cell.getRowIndex() == 1 && cell.getColumnIndex() == 0) {}****
         * 保证每个cell被合并一次，如果不加上面的判断，因为是一个cell一个cell操作的，
         * 例如合并A2:A3,当cell为A2时，合并A2,A3，但是当cell为A3时，又是合并A2,A3，
         * 但此时A2,A3已经是合并的单元格了
         */
        if (CollectionUtils.isNotEmpty(cellRangeAddresss)) {
            if (cell.getRowIndex() == 1 && cell.getColumnIndex() == 0) {
                for (CellRangeAddress item : cellRangeAddresss) {
                    sheet.addMergedRegionUnsafe(item);
                }
            }
        }
    }
}
